package cs575.server.services;

import java.nio.ByteBuffer;

import cs575.common.FantasyMessage;
import cs575.common.Player;
import cs575.common.Player.Position;
import cs575.server.FantasyServer;

/**
 * Service that adds a player to the master server player list. No message is
 * returned.
 */
public class AddPlayer {

	public void execute(FantasyMessage message) {
		ByteBuffer m = ByteBuffer.wrap(message.getMessage());

		int nSize = m.getInt();
		byte[] nBytes = new byte[nSize];
		m.get(nBytes);
		String name = new String(nBytes);

		if (!FantasyServer.doesPlayerExist(name)) {
			Position position = Position.valueOf(m.getInt());
			Player p = new Player(name, position);
			FantasyServer.updatePlayerTable(p);
		}
	}
}
